#import "OmMomentsVCTableViewCell.h"
#import "OmMomentsVCTableViewCellImagesView.h"
#import "Liu70TimeShowLabel.h"
#import "OmBuyKingHatVC.h"
#import "OmMomentsVCCommentView.h"
#import "OmMomentsVC.h"
#import "OmUserPageVC.h"
@interface OmMomentsVCTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (strong, nonatomic) Liu70TimeShowLabel *timeLabel;
@property (strong, nonatomic) OmMomentsVCTableViewCellImagesView *imagesView;
@property (strong, nonatomic) OmMomentsVCCommentView *commentView;
@property (weak, nonatomic) IBOutlet UIView *bottomSeparateView;
@property (weak, nonatomic) IBOutlet UIButton *sayButton;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UILabel *likeLabel;
@property (nonatomic, strong) UIButton *delButton;
@end
@implementation OmMomentsVCTableViewCell {
    BOOL _like;
}

- (void)delTap {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"del_portal"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"uid"] = curUser.id;
    param[@"token"] = curUser.token;
    param[@"portal_id"] = self.model.ID;
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [MBProgressHUD showSuccessMessage:ASLocalizedString(@"删除成功")];
        [self.sd_tableView.mj_header beginRefreshing];
    } failure:^(NSString *error) {
    } hasCache:NO];
}

- (IBAction)likeMoment:(id)sender {
    [self.likeButton setImage:IMAGE_NAMED(_like ? @"OmLikeMoment":@"OmLikeMomentSelected") forState:UIControlStateNormal];
    self.likeLabel.text = [NSString stringWithFormat:@"%d", (self.likeLabel.text.integerValue + (_like?-1:1))];
    _like = !_like;
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"thumbs_up"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"portal_id"] = self.model.ID;
    param[@"status"] = @(_like?1:0);
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"novelty_list"];
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"uid"] = curUser.id;
        param[@"token"] = curUser.token;
        param[@"page"] = @(self.sd_tableView.refreshPage);
        [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        } success:^(id responseObject) {
            NSMutableArray *modelArray = [NoveltyApiNoveltyListModel mj_objectArrayWithKeyValuesArray:responseObject];
            for (NoveltyApiNoveltyListModel *model in modelArray) {
                if ([model.ID isEqualToString:self.model.ID]) {
                    NSMutableArray *newArray = [self.sd_tableView.refreshDataArray mutableCopy];
                    [newArray replaceObjectAtIndex:self.sd_indexPath.row withObject:model];
                    self.sd_tableView.refreshDataArray = newArray;
                }
            }
        } failure:^(NSString *error) {
        } hasCache:NO];
    } failure:^(NSString *error) {
    } hasCache:NO];
}
- (IBAction)postComment:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"评论") message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"发布") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"publish_comment"];
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        param[@"object_id"] = self.model.ID;
        param[@"content"] = alertController.textFields[0].text;
        [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        } success:^(id responseObject) {
            NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"novelty_api" A:@"novelty_list"];
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            param[@"uid"] = curUser.id;
            param[@"token"] = curUser.token;
            param[@"page"] = @(self.sd_tableView.refreshPage);
            [CYNET POST:url parameters:param responseCache:^(id responseObject) {
            } success:^(id responseObject) {
                NSMutableArray *modelArray = [NoveltyApiNoveltyListModel mj_objectArrayWithKeyValuesArray:responseObject];
                for (NoveltyApiNoveltyListModel *model in modelArray) {
                    if ([model.ID isEqualToString:self.model.ID]) {
                        NSMutableArray *newArray = [self.sd_tableView.refreshDataArray mutableCopy];
                        [newArray replaceObjectAtIndex:self.sd_indexPath.row withObject:model];
                        self.sd_tableView.refreshDataArray = newArray;
                        [self.sd_tableView beginUpdates];
                        [self.sd_tableView reloadRowAtIndexPath:self.sd_indexPath withRowAnimation:UITableViewRowAnimationNone];
                        [self.sd_tableView endUpdates];
                    }
                }
            } failure:^(NSString *error) {
            } hasCache:NO];
        } failure:^(NSString *error) {
            if ([error isEqualToString:@"请先开通会员"]) {
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:ASLocalizedString(@"请升级皇冠会员") message:nil preferredStyle:UIAlertControllerStyleAlert];
                [controller addAction:[UIAlertAction actionWithTitle:ASLocalizedString(@"确认") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    OmBuyKingHatVC *vc = [[OmBuyKingHatVC alloc] initWithType:MemberTypeKingHat];
                    [self.viewController.navigationController pushViewController:vc animated:YES];
                }]];
                [self.viewController presentViewController:controller animated:YES completion:nil];
            }
        } hasCache:NO];
    }]];
    [self.viewController presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)goUserPage:(id)sender {
    OmUserPageVC *vc = [[OmUserPageVC alloc] init];
    vc.ID = self.model.user_id;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    self.headImageView.fp_contentMode(UIViewContentModeScaleAspectFill).fp_cornerRadius(25).fp_clipsToBounds(YES);
    self.timeLabel = (Liu70TimeShowLabel *)[[Liu70TimeShowLabel alloc] init]
    .fp_font([UIFont systemFontOfSize:12 weight:UIFontWeightMedium])
    .fp_textColor(HexadecimalColor(@"#999999"));
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.bottom.equalTo(self.headImageView).offset(-4);
    }];
    self.imagesView = [[OmMomentsVCTableViewCellImagesView alloc] init];
    [self.contentView addSubview:self.imagesView];
    self.commentView = [[OmMomentsVCCommentView alloc] init];
    [self.contentView addSubview:self.commentView];
    
    self.delButton = [[UIButton alloc] init];
    self.delButton.titleLabel.font = RPXFont(12);
    [self.delButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.delButton setTitle:ASLocalizedString(@"删除") forState:UIControlStateNormal];
    [self.delButton addTarget:self action:@selector(delTap) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.delButton];
    [self.delButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imagesView.mas_left);
        make.top.equalTo(self.commentView.mas_bottom).offset(7);
    }];
}
- (void)setModel:(NoveltyApiNoveltyListModel *)model {
    _model = model;
    self.nameLabel.text = model.user_nickname;
    self.contentLabel.text = model.post_content;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    self.imagesView.imageModelArray = model.images;
    self.commentView.modelArray = model.child;
    self.likeLabel.text = [NSString stringWithFormat:@"%zd", model.post_like];
    self.timeLabel.timeStamp = model.published_time;
    [self.likeButton setImage:IMAGE_NAMED(self.model.is_like?@"OmLikeMomentSelected":@"OmLikeMoment") forState:UIControlStateNormal];
    _like = model.is_like;
    [self.imagesView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(model.images.count <= 0 ? 0 : 25);
        make.left.equalTo(self.contentView).offset(22);
        make.right.equalTo(self.contentView).offset(-22);
    }];
    [self.commentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imagesView.mas_bottom).offset(model.child.count <= 0 ? 0 : 12);
        make.left.equalTo(self.contentView).offset(22);
        make.right.equalTo(self.contentView).offset(-22);
        make.bottom.equalTo(self.sayButton.mas_top).offset(-10);
    }];
    self.delButton.hidden = !self.isSelf;
}
@end
